package com.sunfeng.chap2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author : 孙峰
 * @Description:
 * @Date : 2021/10/9  13:45
 */
public class Test10 {
    public static void main(String[] args) throws IOException {
        ByteBuffer byteBuffer = ByteBuffer.wrap("12345678".getBytes());
        FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\nio\\newtxt.txt"));
        FileChannel fileChannel = fileOutputStream.getChannel();

        fileChannel.write(byteBuffer);
        System.out.println("A size"+ fileChannel.size()+" position ="+fileChannel.position());
        fileChannel.truncate(3);
        System.out.println("B size"+ fileChannel.size()+" position ="+fileChannel.position());
        fileChannel.close();
        fileOutputStream.flush();
        fileOutputStream.close();
    }
}
